const cookieparser = process.server ? require('cookieparser') : undefined

export const state = () => {
    return {
        user: null
    }
}

export const mutations = {
    setUser (state,data) {
        state.user = data
    }
}

export const actions = {
    nuxtServerInit({commit}, {req}) {
        let auth = null

        //如果请求头中又cookie
        if (req.headers.cookie) {
            const parsed = cookieparser.parse(req.headers.cookie)
            try {
                auth = JSON.parse(parse.auth)
            } catch (err) {

            }
        }
        commit('setUser',user) //提交修改mutation 状态
    }
}